package com.zdb.java.thread;

import org.junit.Test;

public class ThreadTest {

    /**
     * 现在有 T1、T2、T3 三个线程，
     * 你怎样保证 T2 在 T1 执行完后执行，T3 在 T2 执行完后执行？
     */
    @Test
    public void testJoin() throws Exception {

        Thread t1 = new Thread(() -> {
            System.out.println("t1 begin");
            try {
//                Thread.currentThread().wait(0);
                Thread.currentThread().wait(10);
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t1 end");


        });
        Thread t2 = new Thread(() -> {
            System.out.println("t2 begin");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t2 end");
        });
        Thread t3 = new Thread(() -> {
            System.out.println("t3 begin");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t3 end");
        });
        t1.start();
        t1.join();
        t2.start();
        t2.join();
        t3.start();
        t3.join();

        System.out.println("----- over ------");

    }

    @Test
    public void testWait() throws Exception {


        Thread t1 = new Thread(() -> {
//            try {
//                Thread.currentThread().wait(10000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("------ after wait --------- ");
            try {
                synchronized (this) {
                    this.wait(0);
                }
                Thread.sleep(10000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("--- t1 ----");

        });
        t1.start();
        t1.wait(0);
//        try {
//            t1.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println("hehe");



    }
}
